#include "sessiondetailwidget.h"
#include"model/datacenter.h"
#include<QMessageBox>

SessionDetailWidget::SessionDetailWidget(const model::ChatSessionInfo &chatSessionInfo)
    :chatSessionInfo(chatSessionInfo)
{
    // 设置关闭后⾃动销毁
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 1. 设置基本属性
    this->setWindowTitle("会话详情");
    this->setAttribute(Qt::WA_DeleteOnClose);

    this->setWindowIcon(QIcon(":/images/logo.png"));
    this->setFixedSize(300, 300);
    this->setStyleSheet("QWidget {background-color: rgb(255, 255, 255); }");

    // 2. 创建核⼼布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50, 50, 50, 50);
    layout->setSpacing(10);
    layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    this->setLayout(layout);
    // 3. 添加 "添加按钮"
    AvatarItem* addBtn = new AvatarItem(QIcon(":/images/add.png"), "添加");
    layout->addWidget(addBtn, 0, 1);

// 4. 添加当前⽤⼾头像
#if TEST_UI
    AvatarItem* currentUser = new AvatarItem(QIcon(":/images/default_avatar.jpg"), "张三");
    layout->addWidget(currentUser, 0, 1);
#endif

#if LOAD_DATA_FROM_NETWORK
    // 获取到当前的对⽅⽤⼾信息. 对⽅⼀定是咱们的好友.
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* userInfo = dataCenter->getFriendById(chatSessionInfo.userId);
    if (userInfo != nullptr) {
        AvatarItem* currentUser = new AvatarItem(userInfo->avatar, userInfo->nickname);
        layout->addWidget(currentUser, 0, 0);
    }
#endif

    // 5. 添加空⽩
    QSpacerItem* spacerItem = new QSpacerItem(70, 70);
    layout->addItem(spacerItem, 0, 2);
    // 6. 添加 删除好友 按钮
    QPushButton* deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setFixedHeight(50);
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90,90, 90); border-radius: 5px; }\
                                    QPushButton:pressed { background-color:rgb(235, 235, 235); }");
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 3);

    //删除好友
    connect(deleteFriendBtn, &QPushButton::clicked, this, &SessionDetailWidget::clickDeleteFriendBtn);

    //选择好友
    addBtn->setClicked([=]() {
        ChooseFriendDialog* dialog = new ChooseFriendDialog(chatSessionInfo.userId);
        // 弹出模态对话框
        auto result = dialog->exec();
        if (result == QDialog::Accepted) {
            // 关闭当前窗⼝
            this->close();
        }
    });
}

void SessionDetailWidget::clickDeleteFriendBtn()
{
    // 1. 弹出对话框, 确认是否要删除
    auto result = QMessageBox::warning(this, "确认删除", "确认删除该好友?", QMessageBox::Ok | QMessageBox::Cancel);
    if (result != QMessageBox::Ok) {
        LOG() << "删除好友取消";
        return;
    }

    // 2. 发送请求删除好友
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->deleteFriendAsync(chatSessionInfo.userId);
    // 3. 关闭本窗⼝
    this->close();
}

AvatarItem::AvatarItem(const QIcon &avatar, const QString &name)
{
    // 1. 设置基本属性
    this->setFixedSize(70, 80);
    // 2. 创建核⼼布局
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(5);
    this->setLayout(layout);
    // 3. 创建头像. 使⽤按钮表⽰. ⽅便后续实现点击操作.
    avatarBtn = new QPushButton(this);
    avatarBtn->setIcon(avatar);
    avatarBtn->setFixedSize(45, 45);
    avatarBtn->setIconSize(QSize(45, 45));
    avatarBtn->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    avatarBtn->setStyleSheet("QPushButton {border: 1px solid rgb(218, 218,218);}");

    // //点击该按钮就跳到选择好友页面
    // connect(avatarBtn, &QPushButton::clicked, this, [=](){
    //     ChooseFriendDialog* chooseFriendDialog = new ChooseFriendDialog(QString::number(1));
    //     chooseFriendDialog->show();
    // });

    // 4. 创建名字
    nameLabel = new QLabel();
    QFont font("微软雅⿊", 12);
    nameLabel->setFont(font);
    nameLabel->setAlignment(Qt::AlignCenter);
    nameLabel->setText(name);
    // 5. 名字截断逻辑
    QFontMetrics metrics(font);
    int totalWidth = metrics.horizontalAdvance(name);
    const int MAX_WIDTH = 65;

    if (totalWidth >= MAX_WIDTH) {
        // 宽度太⻓了, 截断名字
        QString ellipsis = "...";
        int ellipsisWidth = metrics.horizontalAdvance(ellipsis);
        int availableWidth = MAX_WIDTH - ellipsisWidth;
        // 取左侧 n 个字符
        QString truncatedName = name.left(name.length() * availableWidth / totalWidth);
        nameLabel->setText(truncatedName + ellipsis);
    }
    // 6. 添加到布局管理器中
    layout->addWidget(avatarBtn, 0, Qt::AlignCenter);
    layout->addWidget(nameLabel, 0, Qt::AlignCenter);
}

void AvatarItem::setClicked(std::function<void ()> slotFunc)
{
    connect(avatarBtn, &QPushButton::clicked, this, slotFunc);
}
